<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>【JVM】 相关知识 | 寒冷如铁</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/blog/favicon.ico">
    <script language="javascript" type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script language="javascript" type="text/javascript" src="/blog/js/MouseClickEffect.js"></script>
    <meta name="description" content="好奇就尝试，喜欢就坚持！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog/assets/css/0.styles.01ca31a9.css" as="style"><link rel="preload" href="/blog/assets/js/app.0c46c7d8.js" as="script"><link rel="preload" href="/blog/assets/js/3.5679fe00.js" as="script"><link rel="preload" href="/blog/assets/js/1.8cdd2163.js" as="script"><link rel="preload" href="/blog/assets/js/36.5a46a47a.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.0b6ffd8d.js"><link rel="prefetch" href="/blog/assets/js/11.d27e4c34.js"><link rel="prefetch" href="/blog/assets/js/12.ba5bd991.js"><link rel="prefetch" href="/blog/assets/js/13.ab5bead7.js"><link rel="prefetch" href="/blog/assets/js/14.124a0dc3.js"><link rel="prefetch" href="/blog/assets/js/15.ac521310.js"><link rel="prefetch" href="/blog/assets/js/16.b32290db.js"><link rel="prefetch" href="/blog/assets/js/17.d65c6ccd.js"><link rel="prefetch" href="/blog/assets/js/18.0a7a10ef.js"><link rel="prefetch" href="/blog/assets/js/19.e0e5e91a.js"><link rel="prefetch" href="/blog/assets/js/20.bcc0e382.js"><link rel="prefetch" href="/blog/assets/js/21.59edea58.js"><link rel="prefetch" href="/blog/assets/js/22.8239cecf.js"><link rel="prefetch" href="/blog/assets/js/23.48516c82.js"><link rel="prefetch" href="/blog/assets/js/24.4e1a046f.js"><link rel="prefetch" href="/blog/assets/js/25.56c0a228.js"><link rel="prefetch" href="/blog/assets/js/26.fb764e02.js"><link rel="prefetch" href="/blog/assets/js/27.f96c7435.js"><link rel="prefetch" href="/blog/assets/js/28.31139fa0.js"><link rel="prefetch" href="/blog/assets/js/29.fa31f05f.js"><link rel="prefetch" href="/blog/assets/js/30.49c791b1.js"><link rel="prefetch" href="/blog/assets/js/31.68660a8e.js"><link rel="prefetch" href="/blog/assets/js/32.4e07e3b0.js"><link rel="prefetch" href="/blog/assets/js/33.e08324f8.js"><link rel="prefetch" href="/blog/assets/js/34.9153a266.js"><link rel="prefetch" href="/blog/assets/js/35.49d3bb37.js"><link rel="prefetch" href="/blog/assets/js/37.2403de70.js"><link rel="prefetch" href="/blog/assets/js/38.794a3bde.js"><link rel="prefetch" href="/blog/assets/js/39.185570eb.js"><link rel="prefetch" href="/blog/assets/js/4.a5921925.js"><link rel="prefetch" href="/blog/assets/js/40.b5ddc5b2.js"><link rel="prefetch" href="/blog/assets/js/41.81f131ab.js"><link rel="prefetch" href="/blog/assets/js/42.474cb45b.js"><link rel="prefetch" href="/blog/assets/js/43.fe269b94.js"><link rel="prefetch" href="/blog/assets/js/44.123909bf.js"><link rel="prefetch" href="/blog/assets/js/45.85e17df3.js"><link rel="prefetch" href="/blog/assets/js/46.96eb5367.js"><link rel="prefetch" href="/blog/assets/js/47.2c06ceb7.js"><link rel="prefetch" href="/blog/assets/js/48.dcaa5d75.js"><link rel="prefetch" href="/blog/assets/js/49.47475370.js"><link rel="prefetch" href="/blog/assets/js/5.a6bfa893.js"><link rel="prefetch" href="/blog/assets/js/50.29aa41a9.js"><link rel="prefetch" href="/blog/assets/js/51.b049cb19.js"><link rel="prefetch" href="/blog/assets/js/52.9f16a8d1.js"><link rel="prefetch" href="/blog/assets/js/53.9ccedc0d.js"><link rel="prefetch" href="/blog/assets/js/54.c16fde39.js"><link rel="prefetch" href="/blog/assets/js/55.3d2e78cc.js"><link rel="prefetch" href="/blog/assets/js/56.14b1dddb.js"><link rel="prefetch" href="/blog/assets/js/57.a48eb0da.js"><link rel="prefetch" href="/blog/assets/js/58.db985774.js"><link rel="prefetch" href="/blog/assets/js/59.dbb67461.js"><link rel="prefetch" href="/blog/assets/js/6.d5f7873e.js"><link rel="prefetch" href="/blog/assets/js/60.fd9edd5c.js"><link rel="prefetch" href="/blog/assets/js/61.c87eaa37.js"><link rel="prefetch" href="/blog/assets/js/62.09bd4303.js"><link rel="prefetch" href="/blog/assets/js/63.c3a4cb82.js"><link rel="prefetch" href="/blog/assets/js/64.1e623e6b.js"><link rel="prefetch" href="/blog/assets/js/65.5ee98079.js"><link rel="prefetch" href="/blog/assets/js/66.875fdeb3.js"><link rel="prefetch" href="/blog/assets/js/67.52e4feee.js"><link rel="prefetch" href="/blog/assets/js/68.09bb6522.js"><link rel="prefetch" href="/blog/assets/js/69.e5cc1032.js"><link rel="prefetch" href="/blog/assets/js/7.63c57787.js"><link rel="prefetch" href="/blog/assets/js/70.a2307508.js"><link rel="prefetch" href="/blog/assets/js/71.75bc515a.js"><link rel="prefetch" href="/blog/assets/js/72.f7d10a87.js"><link rel="prefetch" href="/blog/assets/js/73.ae777bb5.js"><link rel="prefetch" href="/blog/assets/js/74.ddd90b9a.js"><link rel="prefetch" href="/blog/assets/js/75.5d297adc.js"><link rel="prefetch" href="/blog/assets/js/76.299aaa21.js"><link rel="prefetch" href="/blog/assets/js/77.c93068f7.js"><link rel="prefetch" href="/blog/assets/js/78.727916c9.js"><link rel="prefetch" href="/blog/assets/js/79.605f2aff.js"><link rel="prefetch" href="/blog/assets/js/8.22284503.js"><link rel="prefetch" href="/blog/assets/js/80.0081929d.js"><link rel="prefetch" href="/blog/assets/js/9.8e870e00.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.01ca31a9.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-19557b78><div data-v-19557b78><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-19557b78 data-v-19557b78><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-19557b78 data-v-19557b78><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>寒冷如铁</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>寒铁</span>
            
          <span data-v-64685f0e>2020 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-19557b78><header class="navbar" data-v-19557b78><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">寒冷如铁</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/build/" class="nav-link"><i class="iconfont undefined"></i>
  build
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/db/" class="nav-link"><i class="iconfont undefined"></i>
  db
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/docker/" class="nav-link"><i class="iconfont undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/workflow/" class="nav-link"><i class="iconfont undefined"></i>
  workflow
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/git/" class="nav-link"><i class="iconfont undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/java/" class="nav-link"><i class="iconfont undefined"></i>
  java
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/source/" class="nav-link"><i class="iconfont undefined"></i>
  source
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/linux/" class="nav-link"><i class="iconfont undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/network/" class="nav-link"><i class="iconfont undefined"></i>
  network
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/project/" class="nav-link"><i class="iconfont undefined"></i>
  project
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/share/" class="nav-link"><i class="iconfont undefined"></i>
  share
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/software/" class="nav-link"><i class="iconfont undefined"></i>
  software
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/spring/" class="nav-link"><i class="iconfont undefined"></i>
  spring
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/tomcat/" class="nav-link"><i class="iconfont undefined"></i>
  tomcat
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/web/" class="nav-link"><i class="iconfont undefined"></i>
  web
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/views/guide.html" class="nav-link"><i class="iconfont reco-other"></i>
  记录
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      外链
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/huhuhan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/huhu_han" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://console.leancloud.app/#/apps" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  有道云笔记
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://console.leancloud.app/#/apps" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  LeadCloud
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://cloud.seafile.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  Seafile
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://huhu_han.gitee.io/downgit" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  DownGit
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-19557b78></div> <aside class="sidebar" data-v-19557b78><div class="personal-info-wrapper" data-v-b038cec6><img src="/blog/avatar.png" alt="author-avatar" class="personal-img" data-v-b038cec6> <h3 class="name" data-v-b038cec6>
    寒铁
  </h3> <div class="num" data-v-b038cec6><div data-v-b038cec6><h3 data-v-b038cec6>66</h3> <h6 data-v-b038cec6>文章</h6></div> <div data-v-b038cec6><h3 data-v-b038cec6>17</h3> <h6 data-v-b038cec6>标签</h6></div></div> <hr data-v-b038cec6></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/build/" class="nav-link"><i class="iconfont undefined"></i>
  build
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/db/" class="nav-link"><i class="iconfont undefined"></i>
  db
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/docker/" class="nav-link"><i class="iconfont undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/workflow/" class="nav-link"><i class="iconfont undefined"></i>
  workflow
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/git/" class="nav-link"><i class="iconfont undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/java/" class="nav-link"><i class="iconfont undefined"></i>
  java
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/source/" class="nav-link"><i class="iconfont undefined"></i>
  source
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/linux/" class="nav-link"><i class="iconfont undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/network/" class="nav-link"><i class="iconfont undefined"></i>
  network
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/project/" class="nav-link"><i class="iconfont undefined"></i>
  project
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/share/" class="nav-link"><i class="iconfont undefined"></i>
  share
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/software/" class="nav-link"><i class="iconfont undefined"></i>
  software
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/spring/" class="nav-link"><i class="iconfont undefined"></i>
  spring
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/tomcat/" class="nav-link"><i class="iconfont undefined"></i>
  tomcat
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/web/" class="nav-link"><i class="iconfont undefined"></i>
  web
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/views/guide.html" class="nav-link"><i class="iconfont reco-other"></i>
  记录
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      外链
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/huhuhan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/huhu_han" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://console.leancloud.app/#/apps" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  有道云笔记
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://console.leancloud.app/#/apps" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  LeadCloud
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://cloud.seafile.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  Seafile
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://huhu_han.gitee.io/downgit" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-other"></i>
  DownGit
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>【JVM】 相关知识</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/views/java/jvm.html#jvm-简介" class="sidebar-link">JVM 简介</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/java/jvm.html#内存结构" class="sidebar-link">内存结构</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/java/jvm.html#运行时数据区" class="sidebar-link">运行时数据区</a></li></ul></li><li><a href="/blog/views/java/jvm.html#垃圾回收机制" class="sidebar-link">垃圾回收机制</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/java/jvm.html#判断存活算法" class="sidebar-link">判断存活算法</a></li><li class="sidebar-sub-header"><a href="/blog/views/java/jvm.html#垃圾回收算法" class="sidebar-link">垃圾回收算法</a></li><li class="sidebar-sub-header"><a href="/blog/views/java/jvm.html#垃圾回收器" class="sidebar-link">垃圾回收器</a></li><li class="sidebar-sub-header"><a href="/blog/views/java/jvm.html#疑问点" class="sidebar-link">疑问点</a></li></ul></li><li><a href="/blog/views/java/jvm.html#调优分析" class="sidebar-link">调优分析</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/java/jvm.html#配置参数" class="sidebar-link">配置参数</a></li><li class="sidebar-sub-header"><a href="/blog/views/java/jvm.html#内存溢出" class="sidebar-link">内存溢出</a></li><li class="sidebar-sub-header"><a href="/blog/views/java/jvm.html#工具应用" class="sidebar-link">工具应用</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-19557b78><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>【JVM】 相关知识</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>寒铁</span>
            
          <span data-v-64685f0e>2020 - </span>
          2022
        </a></span></div></div> <div data-v-19557b78><main class="page"><div class="page-title" style="display:none;"><h1>【JVM】 相关知识</h1> <hr> <div data-v-484a899e><i class="iconfont reco-account" data-v-484a899e><span data-v-484a899e>寒铁</span></i> <i class="iconfont reco-date" data-v-484a899e><span data-v-484a899e>2021-03-09</span></i> <i class="iconfont reco-eye" data-v-484a899e><span id="/blog/views/java/jvm.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-484a899e><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-484a899e><span class="tag-item" data-v-484a899e>
      java
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="jvm-简介"><a href="#jvm-简介" class="header-anchor">#</a> JVM 简介</h2> <p>JVM 即 JAVA 虚拟机 ，由C语言开发。</p> <p>JVM也有不同厂商开发，主要有如下几种</p> <ul><li><strong>HotSpot</strong>，即最常见，Sun 公司开发。后续内容就是这个版本JVM</li> <li>JRockit，是Oracle 公司开发</li> <li>IBM，即IBM公司开发</li></ul> <p>简单例子介绍</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>java demo.class
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ol><li>运行以上命令，操作系统创建一个进程执行JAVA程序</li> <li>JAVA程序会创建JVM，JVM加载运行</li> <li>JAVA通过JNI调用C语言代码实现Socket通信，JVM处理字节码命令</li> <li>进程的虚拟地址空间，分配一部分空间给JVM使用，即<strong>JVM内存</strong></li> <li>进程的剩余其他空间，即进程内存，对JVM来说，统称为<strong>本地内存</strong></li></ol> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20200511161031511.png" alt=""></p> <blockquote><p>参考：<a href="https://didispace-wx.blog.csdn.net/article/details/102889416" target="_blank" rel="noopener noreferrer">博客<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>、<a href="https://www.jianshu.com/p/76959115d486" target="_blank" rel="noopener noreferrer">博客<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <h2 id="内存结构"><a href="#内存结构" class="header-anchor">#</a> 内存结构</h2> <h3 id="运行时数据区"><a href="#运行时数据区" class="header-anchor">#</a> 运行时数据区</h3> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/0b7ead804a1c0ad99a5c72d53775227f_652x625.png" alt=""></p> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20210608142841.png" alt=""></p> <ul><li><p><strong>堆内存</strong>：理解为管理内存的主要区域，多线程共享。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token number">1.</span> 存储的数据包括
	<span class="token number">1.</span> 对象实例（Student对象）
	<span class="token number">2.</span> 数组（数据，非引用）
<span class="token number">2.</span> 垃圾回收负责的主要区域
<span class="token number">3.</span> 逃逸分析（待研究）
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p><strong>方法区</strong>：和堆一样，多线程共享的内存区域。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token number">1.</span> 存储的数据包括
	<span class="token number">1.</span> 虚拟机加载的类（System类）
	<span class="token number">2.</span> 静态变量（static修饰的Student类）
	<span class="token number">3.</span> 常量（NUMBER，在常量池中）
	<span class="token number">4.</span> 即时编译后的class代码（比如动态代理生成的代理类）
	<span class="token number">5</span>、全局对象的引用（stu）？？
<span class="token number">2.</span> 常量池，独立的内存区域，JDK6前在方法区中，JDK7后在堆内存中
<span class="token number">3.</span> 永久代，运行时数据区的<span class="token number">5</span>个区域理解为是JVM的设计规范，永久代是HotSpot虚拟机对方法区的实现方案，JDK7后又取消永久代的设计。
<span class="token number">4.</span> 元空间，JDK8新加，方法区新的实现方案，不过存储位置在本地内存中，不在JVM内存中
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li></ul> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xNTE3ODcxOS0wNjc3NzMwMjkxMmNmZGQ2P2ltYWdlTW9ncjIvYXV0by1vcmllbnQvc3RyaXAlN0NpbWFnZVZpZXcyLzIvdy8xMDAwL2Zvcm1hdC93ZWJw.webp" alt=""></p> <ul><li><p><strong>程序计数器</strong>：理解为存储当前线程的指令步骤。线程私有</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>因为一个CPU在某一时刻只能处理一个线程的一个指令，多线程情况下，CUP需要切换线程处理。保证切换后回到各自线程继续处理指令。运行java方法，存储字节码指令地址；运行native方法，不存储；
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p><strong>虚拟机栈</strong>：理解为Java方法运行的内存模型。线程私有</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token number">1.</span> 一个方法执行对应创建一个栈帧，比如main方法栈帧
<span class="token number">2.</span> 在当前线程虚拟机栈中，栈帧执行顺序：先进后出
<span class="token number">3.</span> 一个栈帧中存储了：
	<span class="token number">1.</span> 局部变量表，存放方法局部变量、基本类型（比如<span class="token number">500</span>、<span class="token number">100</span>）
	<span class="token number">2.</span> 操作数栈（记录操作步骤，比如上图左边的指令表）
	<span class="token number">3.</span> 动态链接（指向常量池的引用）
	<span class="token number">4.</span> 方法返回地址（当前栈帧的出栈，将返回值给上层方法栈帧，继续处理）
	<span class="token number">5.</span> 其他信息
<span class="token number">4.</span> 栈帧大小，在编译阶段已确认，加载到JVM中
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li> <li><p><strong>本地方法栈</strong>：原理同虚拟机栈。是适用于本地方法（Native 关键字修饰的方法）。线程私有</p></li></ul> <h2 id="垃圾回收机制"><a href="#垃圾回收机制" class="header-anchor">#</a> 垃圾回收机制</h2> <p>JAVA GC 即Garbage Collection， 垃圾回收机制，是JVM内存回收机制，简称GC。</p> <p>运行时数据区的5大区域中，主要针对共享区的<strong>堆内存、方法区</strong>，线程私有的内存随着线程销毁回收。</p> <h3 id="判断存活算法"><a href="#判断存活算法" class="header-anchor">#</a> 判断存活算法</h3> <p>GC回收<strong>堆内存、方法区</strong>中的对象，通过判断对象是否存活（死去即不再被引用）</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>引用计数算法
早期算法，这种算法判断很简单，就是给对象添加一个引用计数器，每当对象被引用一次就加<span class="token number">1</span>，引用失效时就减<span class="token number">1</span>。当为<span class="token number">0</span>的时候就判断对象不会再被引用。
优点<span class="token punctuation">:</span>实现简单效率高，被广泛使用与如python何游戏脚本语言上。
缺点<span class="token punctuation">:</span>难以解决循环引用的问题，就是假如两个对象互相引用已经不会再被其它其它引用，导致一直不会为<span class="token number">0</span>就无法进行回收。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>可达性分析算法</strong>：通过一个称为“GC Roots”的对象为<strong>起始点</strong>，搜索所经过的路径称为<strong>引用链</strong>，当一个对象到GC Roots<strong>没有任何引用跟它连接</strong>，则证明<strong>对象是不可用</strong>的。有效解决了引用计数算法的循环利用弊端。</p> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/10006199-854e1de91f66764b.webp" alt=""></p> <p>可作为GC Roots的对象有四种</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token number">1.</span> 虚拟机栈<span class="token punctuation">(</span>栈桢中的本地变量表<span class="token punctuation">)</span>中的引用的对象。
<span class="token number">2.</span> 方法区中的类静态属性引用的对象，一般指被static修饰引用的对象，加载类的时候就加载到内存中。
<span class="token number">3.</span> 方法区中的常量引用的对象<span class="token punctuation">,</span>
<span class="token number">4.</span> 本地方法栈中JNI（native方法<span class="token punctuation">)</span>引用的对象
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="垃圾回收算法"><a href="#垃圾回收算法" class="header-anchor">#</a> 垃圾回收算法</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code>为了高效的回收，jvm将堆分为三个区域
<span class="token number">1.</span> 新生代（Young Generation）NewSize和MaxNewSize分别可以控制新生代的初始大小和最大的大小
<span class="token number">2.</span> 老年代（Old Generation）
<span class="token number">3.</span> 永久代（Permanent Generation）【<span class="token number">1.8</span>以后采用元空间Metaspace，就不在堆中了】
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>​	<img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20f61812fe7ebd758273caad400e7586_1010x418.png" alt=""></p> <p>JVM采用<code>分代收集算法</code>对不同区域、不同大小对象、不同对象存活时长，采用不同的回收算法。</p> <p>新生代，采用<strong>复制算法</strong>。大部分对象存活率低，少量对象的区域复制就完成清除。</p> <ul><li>优先在Eden区分配，其次From Survivor区，To Survivor区为空。（两个Survivor区，简称From 区和To 区）
<ul><li>当Eden区不够空间分配，发起一次Minor GC</li> <li>Eden区存活对象复制到To 区</li> <li>From区，根据存活年龄，分到To 区或老年代</li></ul></li></ul> <p>老年代，采用<strong>标记/整理</strong>或<strong>标记/清除</strong>算法</p> <ul><li><p>新生代复制而来的存活对象</p></li> <li><p>直接进入老年代的大对象（避免内存复制）</p></li> <li><p>老年代不足空间，进行Full GC回收。（优化之一就是尽量避免触发Full GC）</p></li> <li><p>通过对存活对象的标记，整理或直接清除，得到连续的空闲内存</p> <p><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_jpg/CmnN7FKTMS4hD4f8AB83b5cbGQQRCORdKgpyy6w4gphcxHe5InB3tH89vHfEeS5YpTVInmpYxrfRI9rjrsTWHw/640?wx_fmt=jpeg" alt="img"></p></li></ul> <h3 id="垃圾回收器"><a href="#垃圾回收器" class="header-anchor">#</a> 垃圾回收器</h3> <p>即垃圾回收的具体实现，针对不同场景和配置，GC采用不同的回收器，可配合使用，如图</p> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/b13f9fea4441821a7511457e3e6d10a8_892x563.png" alt=""></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>新生代
Serial：单线程操作，回收时停止其他线程，结束后其他线程再工作。简单高效。采用复制算法。
ParNew：多线程操作，Serial升级版，可以配合CMS回收器一起用。
Parallel Scavenge：多线程操作，侧重吞吐量
【吞吐量 <span class="token operator">=</span> 代码运行时间 <span class="token operator">/</span> <span class="token punctuation">(</span>代码运行时间 <span class="token operator">+</span> 垃圾收集时间<span class="token punctuation">)</span> 如果代码运行<span class="token number">100</span>min垃圾收集<span class="token number">1</span>min，则为<span class="token number">99</span><span class="token operator">%</span>】

老年代，
Serial Old：单线程操作，类似Serial，但算法不同（标记<span class="token operator">-</span>整理算法）
Parallel Old：多线程操作
CMS：支持并发，采用标记<span class="token operator">-</span>清除算法，生命周期（初始标记<span class="token operator">-</span>并发标记<span class="token operator">-</span>重新标记<span class="token operator">-</span>并发清除）
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code>最新G1回收器
<span class="token number">1.</span> JDK1<span class="token punctuation">.</span><span class="token number">7</span>出现，理解为CMS的升级版，解决CMD产生的空间碎片（压缩空间的功能）。
<span class="token number">2.</span> 算法是标记<span class="token operator">-</span>整理 <span class="token operator">+</span> 复制。
<span class="token number">3.</span> 混合使用，不属于某分区。
<span class="token number">4.</span> 默认关闭（和JDK版本有关），开启配置【<span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>UseG1GC】，
<span class="token number">5.</span> 将堆内存区域化，标记为若干个【region】，配置参数【<span class="token operator">-</span>XX<span class="token punctuation">:</span>G1HeapRegionSize<span class="token operator">=</span>n】默认自动分配（<span class="token number">2</span>的幂）
<span class="token number">6.</span> 采用并发标记、并发复制操作，降低停顿时间。最大停顿时间【<span class="token operator">-</span>XX<span class="token punctuation">:</span>MaxGCPauseMillis<span class="token operator">=</span><span class="token number">200</span>】默认<span class="token number">200</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code>回收模式
<span class="token number">1.</span> Minor GC<span class="token punctuation">:</span> 在新生代<span class="token punctuation">(</span>包括Eden区和Survivor区<span class="token punctuation">)</span>中的垃圾回收。默认经过<span class="token number">15</span>次后还存活就复制到老年代。
<span class="token number">2.</span> Major GC<span class="token punctuation">:</span> 在老年代的垃圾回收，有时需要将新生代清理将部分对象复制而来，类似Full GC
<span class="token number">3.</span> Full GC<span class="token punctuation">:</span> 
	<span class="token number">1.</span> 所有区域的回收，包括新生代、老年代、元空间等等。
	<span class="token number">2.</span> 【System<span class="token punctuation">.</span><span class="token function">gc</span><span class="token punctuation">(</span><span class="token punctuation">)</span>】禁止直接使用。
	<span class="token number">3.</span> 老年代空间不足会触发。优化目标就是避免触发。
<span class="token number">4.</span> Mixed GC<span class="token punctuation">:</span> 仅G1回收器才有。回收新生代和部分老年代
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/73230d3cdceb46300d51f8aaf5f262d3eca.jpg" alt=""></p> <h3 id="疑问点"><a href="#疑问点" class="header-anchor">#</a> 疑问点</h3> <ul><li><p>标记算法中，如何判断对象是否被标记？</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>通过JVM提供的OOPMap数据结构，存储引用链上引用过的位置，避免全局扫描
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>什么是空间分配担保？</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>在minor gc前，jvm会先检查老年代最大可用空间是否大于新生代所有对象总空间，如果是的话，则minor gc可以确保是安全的
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>为什么新生代的复制算法分2个Survivor区？</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>保证复制算法运行的效率，直接复制去老年代可能导致内存不足，直接触发Full GC。且老年代的对象是经过多次可达性分析算法存活的对象。（默认<span class="token number">15</span>次）
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>为什么是Stop the World？</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>进行GC操作时，停止除GC外的所有线程。（侧重高吞吐的回收器，需要减短停顿时间）
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul> <h2 id="调优分析"><a href="#调优分析" class="header-anchor">#</a> 调优分析</h2> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/8128f56458ac53d496d0d7a18f75ba61_2256x1058.png" alt=""></p> <h3 id="配置参数"><a href="#配置参数" class="header-anchor">#</a> 配置参数</h3> <blockquote><ul><li><code>-</code>： 标准VM选项，VM规范的选项。</li> <li><code>-X</code>： 非标准VM选项，不保证所有VM支持。</li> <li><code>-XX</code>： 高级选项，高级特性，但属于不稳定的选项。</li></ul> <p>老年代需要计算，空间大小 = 堆空间大小 - 新生代大空间大小</p></blockquote> <p>常见参数：</p> <ul><li><code>-Xms</code>设置堆的最小空间大小。</li> <li><code>-Xmx</code>设置堆的最大空间大小。</li> <li><code>-Xmn</code>堆中新生代初始及最大大小（NewSize和MaxNewSize为其细化）。</li> <li><code>-XX:NewSize</code>设置新生代最小空间大小。</li> <li><code>-XX:MaxNewSize</code>设置新生代最大空间大小。</li> <li><code>-XX:PermSize</code>设置永久代最小空间大小。（jdk1.8后<code>permSize</code>替换为<code>metaspaceSize</code>）</li> <li><code>-XX:MaxPermSize</code>设置永久代最大空间大小。（jdk1.8后<code>permSize</code>替换为<code>MaxMetaspaceSize</code>）</li> <li><code>-Xss</code>设置每个线程的堆栈大小。</li></ul> <p>其他参数</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>【<span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>PrintGCDetails】 打印GC日志
【<span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>PrintGCDateStamps】 GC日志加上时间戳
【<span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>PrintGCTimeStamps】 GC每次输出日志的计时（从<span class="token number">0</span>开始）
【<span class="token operator">-</span>Xloggc<span class="token punctuation">:</span><span class="token punctuation">.</span><span class="token operator">/</span>yh_gc<span class="token punctuation">.</span>log】 GC日志文件路径（当前路径）
【<span class="token operator">-</span>XX<span class="token punctuation">:</span>UseParNewGC】使用ParNew垃圾回收器，其他参考垃圾回收器的种类
【<span class="token operator">-</span>XX<span class="token punctuation">:</span>UseG1GC】 使用G1垃圾回收器，适合堆内存<span class="token number">2</span>G起
【<span class="token operator">-</span>XX<span class="token punctuation">:</span>MaxGCPauseMillis】 G1垃圾回收器的停顿时长，毫秒
【<span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>ParallelRefProcEnabled】 开启并行处理引用对象
【<span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>HeapDumpOnOutOfMemoryError】 发成OOM时生成DUMP文件
【<span class="token operator">-</span>XX<span class="token punctuation">:</span>HeapDumpPath】 DUMP文件路径
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>4核8G服务器JDK8的JVM配置参考</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token operator">-</span>Xmx5440M <span class="token operator">-</span>Xms5440M \
<span class="token operator">-</span>XX<span class="token punctuation">:</span>MaxMetaspaceSize<span class="token operator">=</span><span class="token number">512</span>M <span class="token operator">-</span>XX<span class="token punctuation">:</span>MetaspaceSize<span class="token operator">=</span><span class="token number">512</span>M \
<span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>UseG1GC <span class="token operator">-</span>XX<span class="token punctuation">:</span>MaxGCPauseMillis<span class="token operator">=</span><span class="token number">100</span> <span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>ParallelRefProcEnabled \
<span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>HeapDumpOnOutOfMemoryError <span class="token operator">-</span>XX<span class="token punctuation">:</span>HeapDumpPath<span class="token operator">=</span>$<span class="token number">1</span>_dump<span class="token punctuation">.</span>hprof \
<span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>PrintGCDetails <span class="token operator">-</span>XX<span class="token punctuation">:</span><span class="token operator">+</span>PrintGCDateStamps <span class="token operator">-</span>Xloggc<span class="token punctuation">:</span>$<span class="token number">1</span>_gc_trace<span class="token punctuation">.</span>log
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="内存溢出"><a href="#内存溢出" class="header-anchor">#</a> 内存溢出</h3> <p>OOM，即<code>OutOfMemeyError</code>，常见以下几种情况：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code> <span class="token class-name">Exception</span> in thread “main”<span class="token operator">:</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>OutOfMemoryError</span><span class="token operator">:</span> <span class="token class-name">Java</span> heap space
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code> <span class="token class-name">Exception</span> in thread “main”<span class="token operator">:</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>OutOfMemoryError</span><span class="token operator">:</span> <span class="token class-name">PermGen</span> space
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>虚拟机栈、本地方法栈：
<ul><li>存在多线程时，栈帧空间不够分配，抛出OOM</li> <li>仅单线程时，栈帧空间不够分配，抛出<code>StackOverflowError</code></li></ul></li></ul> <p>常见的<code>OutOfMemeyError</code>类型如下：</p> <ul><li><p>对象不能被分配到堆内存中</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Exception</span> in thread “main”<span class="token operator">:</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>OutOfMemoryError</span><span class="token operator">:</span> <span class="token class-name">Java</span> heap space
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>类或者方法不能被加载到老年代。它可能出现在一个程序加载很多类的时候，比如引用了很多第三方的库。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Exception</span> in thread “main”<span class="token operator">:</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>OutOfMemoryError</span><span class="token operator">:</span> <span class="token class-name">PermGen</span> space
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>创建的数组大于堆内存的空间</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Exception</span> in thread “main”<span class="token operator">:</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>OutOfMemoryError</span><span class="token operator">:</span> <span class="token class-name">Requested</span> array size exceeds VM limit
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>分配本地分配失败。JNI、本地库或者Java虚拟机都会从本地堆中分配内存空间。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Exception</span> in thread “main”<span class="token operator">:</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>OutOfMemoryError</span><span class="token operator">:</span> request <span class="token generics"><span class="token punctuation">&lt;</span>size<span class="token punctuation">&gt;</span></span> bytes <span class="token keyword">for</span> <span class="token generics"><span class="token punctuation">&lt;</span>reason<span class="token punctuation">&gt;</span></span><span class="token punctuation">.</span> <span class="token class-name">Out</span> of swap space<span class="token operator">?</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>同样是本地方法内存分配失败，只不过是JNI或者本地方法或者Java虚拟机发现。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Exception</span> in thread “main”<span class="token operator">:</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>OutOfMemoryError</span><span class="token operator">:</span> <span class="token generics"><span class="token punctuation">&lt;</span>reason<span class="token punctuation">&gt;</span></span> <span class="token generics"><span class="token punctuation">&lt;</span>stack trace<span class="token punctuation">&gt;</span></span>（<span class="token class-name">Native</span> method）
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul> <h3 id="工具应用"><a href="#工具应用" class="header-anchor">#</a> 工具应用</h3> <p>JDK自带的<code>jconsole.exe</code></p> <p><img src="https://fastly.jsdelivr.net/gh/huhuhan/img@master/md/pic/20210609113055.png" alt=""></p> <p>JDK自带的<code>jstat.exe</code></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>D:<span class="token punctuation">\</span>Program Files<span class="token punctuation">\</span>jdk<span class="token punctuation">\</span>8u251<span class="token punctuation">\</span>bin<span class="token operator">&gt;</span>jstat -gcutil <span class="token number">17232</span>
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT
 <span class="token number">24.38</span>   <span class="token number">0.00</span>  <span class="token number">64.51</span>  <span class="token number">68.26</span>  <span class="token number">96.88</span>  <span class="token number">94.09</span>     <span class="token number">22</span>    <span class="token number">0.046</span>     <span class="token number">2</span>    <span class="token number">0.046</span>    <span class="token number">0.092</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code>S0<span class="token punctuation">:</span> 新生代<span class="token punctuation">,</span>survivor space0简称 就是准备复制的那块 单位为<span class="token operator">%</span>
S1<span class="token punctuation">:</span> 指新生代s1已使用百分比，为<span class="token number">0</span>的话说明没有存活对象到这边
E<span class="token punctuation">:</span> 新生代eden区域<span class="token punctuation">(</span><span class="token operator">%</span><span class="token punctuation">)</span>
O<span class="token punctuation">:</span> 老年代<span class="token punctuation">(</span><span class="token operator">%</span><span class="token punctuation">)</span>
M<span class="token punctuation">:</span> 元空间<span class="token punctuation">(</span><span class="token operator">%</span><span class="token punctuation">)</span>
CCS<span class="token punctuation">:</span> 
YGC<span class="token punctuation">:</span> Yang gc 次数
YGCT<span class="token punctuation">:</span> Yang gc 耗时<span class="token punctuation">(</span>秒<span class="token punctuation">)</span>
FGC<span class="token punctuation">:</span> Full gc 次数
FGCT<span class="token punctuation">:</span> Full gc 耗时<span class="token punctuation">(</span>秒<span class="token punctuation">)</span>
GCT<span class="token punctuation">:</span> YGCT<span class="token operator">+</span>FGCT 耗时<span class="token punctuation">(</span>秒<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2022/5/27 上午11:15:53</span></div></footer> <!----> <!----></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/blog/assets/js/app.0c46c7d8.js" defer></script><script src="/blog/assets/js/3.5679fe00.js" defer></script><script src="/blog/assets/js/1.8cdd2163.js" defer></script><script src="/blog/assets/js/36.5a46a47a.js" defer></script>
  </body>
</html>
